package org.squirrel.modules.bigscreen.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.squirrel.modules.bigscreen.code.ResponseCode;
import org.squirrel.modules.bigscreen.constant.BaseOperationEnum;
import org.squirrel.modules.bigscreen.dto.ReportDto;
import org.squirrel.modules.bigscreen.entity.Report;
import org.squirrel.modules.bigscreen.entity.ReportDashboard;
import org.squirrel.modules.bigscreen.entity.ReportDashboardWidget;
import org.squirrel.modules.bigscreen.entity.ReportExcel;
import org.squirrel.modules.bigscreen.enums.ReportTypeEnum;
import org.squirrel.modules.bigscreen.exception.BusinessException;
import org.squirrel.modules.bigscreen.exception.BusinessExceptionBuilder;
import org.squirrel.modules.bigscreen.mapper.ReportMapper;
import org.squirrel.modules.bigscreen.service.ReportDashboardService;
import org.squirrel.modules.bigscreen.service.ReportDashboardWidgetService;
import org.squirrel.modules.bigscreen.service.ReportExcelService;
import org.squirrel.modules.bigscreen.service.ReportService;
import org.squirrel.modules.bigscreen.util.GaeaBeanUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

@Service
public class ReportServiceImpl extends ServiceImpl<ReportMapper, Report> implements ReportService {

    @Autowired
    private ReportDashboardService reportDashboardService;
    @Autowired
    private ReportDashboardWidgetService reportDashboardWidgetService;
    @Autowired
    private ReportExcelService reportExcelService;

    public void processBatchBeforeOperation(List<Report> entities, BaseOperationEnum operationEnum) throws BusinessException {
        switch (operationEnum) {
            case DELETE_BATCH:
                entities.forEach(report -> {
                    String id = report.getId();
                    Report delReport = this.getById(id);
                    if (null == delReport) {
                        return;
                    }
                    String reportCode = delReport.getReportCode();
                    String reportType = delReport.getReportType();
                    switch (ReportTypeEnum.valueOf(reportType)) {
                        case report_screen:
                            LambdaQueryWrapper<ReportDashboard> reportDashboardLambdaQueryWrapper = Wrappers.lambdaQuery();
                            reportDashboardLambdaQueryWrapper.eq(ReportDashboard::getReportCode, reportCode);
                            reportDashboardService.remove(reportDashboardLambdaQueryWrapper);

                            LambdaQueryWrapper<ReportDashboardWidget> reportDashboardWidgetLambdaQueryWrapper = Wrappers.lambdaQuery();
                            reportDashboardWidgetLambdaQueryWrapper.eq(ReportDashboardWidget::getReportCode, reportCode);
                            reportDashboardWidgetService.remove(reportDashboardWidgetLambdaQueryWrapper);

                            break;
                        case report_excel:
                            LambdaQueryWrapper<ReportExcel> reportExcelLambdaQueryWrapper = Wrappers.lambdaQuery();
                            reportExcelLambdaQueryWrapper.eq(ReportExcel::getReportCode, reportCode);
                            reportExcelService.remove(reportExcelLambdaQueryWrapper);
                            break;
                        default:
                    }
                });
                break;
            default:

        }
    }

    /**
     * 下载次数+1
     *
     * @param reportCode
     */
    @Override
    public void downloadStatistics(String reportCode) {
    	QueryWrapper<Report> queryWrapper = new QueryWrapper<Report>();
        queryWrapper.eq("report_code", reportCode);
        
        Report report = getOne(queryWrapper);
        if (null != report) {
            Long downloadCount = report.getDownloadCount();
            if (null == downloadCount) {
                downloadCount = 0L;
            }else {
                downloadCount++;
            }
            report.setDownloadCount(downloadCount);
            updateById(report);
        }

    }

    @Override
    public void copy(ReportDto dto) {
        if (null == dto.getId()) {
            throw BusinessExceptionBuilder.build(ResponseCode.NOT_NULL, "id");
        }
        if (StringUtils.isBlank(dto.getReportCode())) {
            throw BusinessExceptionBuilder.build(ResponseCode.NOT_NULL, "报表编码");
        }
        Report report = this.getById(dto.getId());
        String reportCode = report.getReportCode();
        Report copyReport = copyReport(report, dto);
        //复制主表数据
        save(copyReport);
        String copyReportCode = copyReport.getReportCode();
        String reportType = report.getReportType();
        switch (ReportTypeEnum.valueOf(reportType)) {
            case report_screen:
                //查询看板
            	QueryWrapper<ReportDashboard> queryWrapper = new QueryWrapper<ReportDashboard>();
                queryWrapper.eq("report_code", reportCode);
                ReportDashboard reportDashboard = reportDashboardService.getOne(queryWrapper);
                if (null != reportDashboard) {
                    reportDashboard.setId(null);
                    reportDashboard.setReportCode(copyReportCode);
                    reportDashboardService.save(reportDashboard);
                }

                //查询组件
                QueryWrapper<ReportDashboardWidget> queryWrappeReportDashboardWidget = new QueryWrapper<ReportDashboardWidget>();
                queryWrappeReportDashboardWidget.eq("report_code", reportCode);
                
                List<ReportDashboardWidget> reportDashboardWidgetList = reportDashboardWidgetService.list(queryWrappeReportDashboardWidget);
                if (!CollectionUtils.isEmpty(reportDashboardWidgetList)) {
                    String finalCopyReportCode = copyReportCode;
                    reportDashboardWidgetList.forEach(reportDashboardWidget -> {
                        reportDashboardWidget.setId(null);
                        reportDashboardWidget.setReportCode(finalCopyReportCode);
                    });
                    reportDashboardWidgetService.saveBatch(reportDashboardWidgetList);
                }

                break;
            case report_excel:
            	QueryWrapper<ReportExcel> queryWrappeReportExcel = new QueryWrapper<ReportExcel>();
            	queryWrappeReportExcel.eq("report_code", reportCode);
                
                ReportExcel reportExcel = reportExcelService.getOne(queryWrappeReportExcel);
                if (null != reportExcel) {
                    reportExcel.setId(null);
                    reportExcel.setReportCode(copyReportCode);
                    reportExcelService.save(reportExcel);
                }

                break;
            default:
        }
    }

    private Report copyReport(Report report, ReportDto dto){
        //复制主表数据
        Report copyReport = new Report();
        GaeaBeanUtils.copyAndFormatter(report, copyReport);
        copyReport.setReportCode(dto.getReportCode());
        copyReport.setReportName(dto.getReportName());
        return copyReport;
    }
}
